<?php
namespace app;

// 应用请求对象类
class Request extends \think\Request
{
    protected $filter = ['strip_tags','htmlspecialchars'];

    /**
     * 获取当前请求的完整pathinfo
     * @param bool $withApp
     * @param bool $withSubfix
     * @return string
     */
    public function completePathInfo(bool $withApp = false, bool $withSubfix = false): string
    {
        $sd = config('route.pathinfo_depr');
        $ext = config('route.url_html_suffix');

        $pathInfo = trim($this->pathinfo() ?: $this->defaultPathInfo(), $sd);
        $pathInfo = !strpos($pathInfo,$sd) ? $pathInfo . $sd . config('route.default_action') : $pathInfo;
        $pathInfo = $withApp ? app('http')->getName() .$sd. $pathInfo : $pathInfo;
        $pathInfo = $withSubfix && !empty($ext)? $pathInfo . '.' . $ext : $pathInfo;
        return $pathInfo;
    }

    /**
     * 获取当前应用默认的完整pathinfo
     * @param bool $withApp
     * @param bool $withSubfix
     * @return string
     */
    public function defaultPathInfo(bool $withApp = false, bool $withSubfix = false): string
    {
        $sd = config('route.pathinfo_depr');
        $ext = config('route.url_html_suffix');

        $pathInfo = config('route.default_controller') . $sd . config('route.default_action');
        $pathInfo = $withApp ? app('http')->getName() .$sd. $pathInfo : $pathInfo;
        $pathInfo = $withSubfix && !empty($ext)? $pathInfo . '.' . $ext : $pathInfo;
        return $pathInfo;
    }

    /**
     * 获取默认应用的完整pathinfo
     * @param bool $withApp
     * @param bool $withSubfix
     * @return string
     */
    public function indexPathInfo(bool $withApp = true, bool $withSubfix = false): string{
        $sd = config('route.pathinfo_depr');
        $ext = config('route.url_html_suffix');

        $pathInfo = config('route.default_controller') . $sd . config('route.default_action');
        $pathInfo = $withApp ? config('app.default_app') .$sd. $pathInfo : $pathInfo;
        $pathInfo = $withSubfix && !empty($ext)? $pathInfo . '.' . $ext : $pathInfo;
        return $pathInfo;
    }

    /**
     * 返回url根路径
     * @return string
     */
    public function rootPathInfo(){
        return '/';
    }

    /**
     * 获取登录页
     */
    public function loginUrl(){
        return (string)url('index/index/login')
            ->suffix(config('route.url_html_suffix'));
    }

}
